package mafiagame.roles;

import java.util.List;

import mafiagame.Action;
import mafiagame.Game;
import mafiagame.Mark;
import mafiagame.Mark.MarkScope;
import mafiagame.Mark.MarkType;
import mafiagame.Player;
import mafiagame.Role;
import mafiagame.RoleDefinition;

public class Detective extends RoleDefinition
{
	public DefinitionType getDefinitionType()
	{
		return DefinitionType.INVESTIGATIVE;
	}

	public String explainRole()
	{
		return "You are the detective! Creeping around every night, learning "
				+ "things that are best left uncovered..."
				+ " You may investigate"
				+ " a single player every night (not yourself) to get their role "
				+ "and alignment.";
	}

	public Detective()
	{
		super();
	}

	public void assignRole(Game g, Player p)
	{
		p.roles.add(new DetectiveRole(g, p));
	}
	
	public String getName()
	{
		return "DETECTIVE";
	}

	public static class DetectiveRole extends Role
	{
		public class InvestigateAction extends Action
		{
			@Override
			public String getDescription()
			{
				return "This will allow you to <b>investigate</b> a player. Investigating a player, unless the "
						+ "investigation is blocked, will give you information about their role and alignment. Use"
						+ " this information wisely!";
			}

			@Override
			public String getCommand()
			{
				return "investigate";
			}

			@Override
			public boolean Execute(String message)
			{
				String[] tokens = message.split(" ");
				if (tokens.length != 2)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"The investigation must be of the format 'investigate [number]'");
					return false;
				}
				Player p = myGame.parsePlayer(tokens[1]);
				if (p == null)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"You must choose a player who is actually playing.");
					return false;
				}
				if (!p.living)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You must choose a player who is alive.");
					return false;
				}

				if (p == myPlayer)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You cannot investigate yourself.");
					return false;
				}

				Mark m = new Mark();
				m.type = MarkType.INVESTIGATE_ALIGNMENT;
				m.duration = 1;
				m.scope = MarkScope.NIGHT;
				m.isBlockable = true;
				m.isRedirectable = false;
				m.source = myPlayer;
				p.marks.add(m);

				Mark n = new Mark();
				n.type = MarkType.INVESTIGATE_ROLE;
				n.duration = 1;
				n.scope = MarkScope.NIGHT;
				n.isBlockable = true;
				n.isRedirectable = false;
				n.source = myPlayer;
				p.marks.add(n);

				myGame.communicator.sendPrivateMessage(myPlayer.username,
						"Investigating <b>" + p.username + "</b> tonight!");

				return true;
			}

			@Override
			public boolean isPublic()
			{
				return false;
			}

		}

		public DetectiveRole(Game g, Player p)
		{
			super(g, p);
		}

		public String getName()
		{
			return "DETECTIVE";
		}

		public String getHelp()
		{
			return "You are the detective! Creeping around every night, learning things that are best left uncovered..."
					+ " You may investigate a single player every night (not yourself) to get their role and alignment.";
		}

		@Override
		public void SetupPhase()
		{

		}

		@Override
		public void NightPhase()
		{
			myGame.addAction(myPlayer.username,
					new InvestigateAction(), false);
		}

		@Override
		public void DayPhase()
		{

		}
	}

}
